import multiprocessing
import os
import time


# 唱歌
def sing():
    for i in range(3):
        print("唱歌...")
        time.sleep(0.5)


# 跳舞
def dance():
    for i in range(3):
        print("跳舞...")
        time.sleep(0.5)


def sing_2(num: int = 3, name: str = "中国人"):
    for i in range(num):
        print("唱歌:{%s}..." % name)
        time.sleep(0.5)


# 跳舞
def dance_2(num: int = 3):
    for i in range(num):
        print("跳舞...")
        time.sleep(0.5)


#  获取进程编号
def sing_3():
    # 进程ID
    print('sing_3进程编号：', os.getpid())
    # 父进程ID
    print('sing_3进程父进程编号：', os.getppid())
    for i in range(3):
        print("唱歌...")
        time.sleep(0.5)


# 跳舞
def dance_3():
    print('dance_3进程编号：', os.getpid())
    print('dance_3进程父进程编号：', os.getppid())
    for i in range(3):
        print("跳舞...")
        time.sleep(0.5)


def work():
    print("work start...")
    time.sleep(5)
    print("work end...")


def copy_file(file_name, source_dir, dest_dir):
    source_file = os.path.join(source_dir, file_name)
    dest_file = os.path.join(dest_dir, file_name)
    with open(source_file, 'rb') as read_file:
        with open(dest_file, 'wb') as write_file:
            while True:
                content = read_file.read(1024)
                if content:
                    write_file.write(content)
                else:
                    break
def copy_Files(source_dir, dest_dir):
    # 创建文件夹
    try:
        os.makedirs(dest_dir)
    except:
        print('目标文件夹已存在')

    # 读取源文件夹的文件列表
    file_list = os.listdir(source_dir)

    for file_name in file_list:
        # # 单进程copy
        # copy_file(file_name, source_dir, dest_dir)
        # 多进程，有参数
        p = multiprocessing.Process(target=copy_file, args=(file_name, source_dir, dest_dir))
        p.start()


if __name__ == '__main__':
    # # 多进程，无参数
    # sing_process = multiprocessing.Process(target=sing)
    # sing_dance = multiprocessing.Process(target=dance)
    # sing_dance.start()
    # sing_process.start()

    # # 两种传参方式（位置参数，命名参数）
    # multiprocessing.Process(target=sing_2, args=(3,)).start()
    # multiprocessing.Process(target=dance_2, kwargs={'num': 4}).start()

    # # 获取进程编号
    # print('main进程编号：', os.getpid())
    # multiprocessing.Process(target=sing_3).start()
    # multiprocessing.Process(target=dance_3).start()

    # # 主进程 会等待 非守护子进程执行完成 才结束
    # multiprocessing.Process(target=work).start()
    # print('主进程完成执行')

    # # 主进程 不等待 守护子进程执行完成
    # work_process = multiprocessing.Process(target=work,daemon=True)
    # # work_process.daemon = True
    # print('主进程完成执行')


    copy_Files('./source', './dest')
